package com.woniuxy.project115.kaer.tenant.config;

import com.woniuxy.project115.kaer.tenant.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @author HuH
 * @version 1.0
 * @CreatTime 2025-02-19 19:59
 * @description
 */
@Configuration("tenantWebConfig")
public class WebConfig implements WebMvcConfigurer {
    @Resource
    private LoginInterceptor loginInterceptor;

//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**") // 所有接口
//                .allowCredentials(true) // 是否发送 Cookie
//                .allowedOrigins("*") // 支持域
//                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
//                .allowedHeaders("*")
//                .exposedHeaders("*");
//    }

    /**
     * @description: 将自定义拦截器注册到拦截器注册中心
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/serviceOrder/noti")
                .excludePathPatterns("/applicationAttachment/getAttachmentById")
                .excludePathPatterns("/settledApplication/uploadLogo","/settledApplication/approveApplication","/settledApplication/getPendingApplication")
                .excludePathPatterns("/serviceInfo/uploadImage")
                .excludePathPatterns("/tenantInfo/login", "/tenantInfo/register","/tenantInfo/sendRegisterCode", "/tenantInfo/phoneLogin", "/tenantInfo/sendLoginCode", "/tenantInfo/codeLogin")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new Converter<String, LocalDate>() {
            @Override
            public LocalDate convert(String s) {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                return LocalDate.parse(s, formatter);
            }
        });
    }
}
